package com.quarkus.pgsql.resources;

import com.quarkus.pgsql.entities.AppUserEntity;
import com.quarkus.pgsql.repositories.AppUserRepository;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

import java.util.List;

@Path("appUser")
public class AppUserResource {

    @Inject
    AppUserRepository repository;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> list() {
        return repository.listAll();
    }

    @GET
    @Path("{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public AppUserEntity findByLoginName(@PathParam("name") String name) {
        return repository.findByLoginName(name);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Transactional
    public void add(AppUserEntity appUser) {
        repository.persist(appUser);
    }

    @GET
    @Path("{userGroup}/{state}/1")
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> getByUseGroupAndState(@PathParam("userGroup") String ug,
                                                     @PathParam("state") String state) {
        return repository.findByUserGroupAndState(ug, state);
    }

    @GET
    @Path("{userGroup}/{state}/2")
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> getByUseGroupAndState2(@PathParam("userGroup") String ug,
                                                      @PathParam("state") String state) {
        return repository.findByUserGroupAndState2(ug, state);
    }

    @GET
    @Path("page/{pageIdx}/{pageSize}/1")
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> findByPage1(@PathParam("pageIdx") Integer currPage,
                                           @PathParam("pageSize") Integer size) {
        return repository.page(size, currPage);
    }

    @GET
    @Path("page/{pageIdx}/{pageSize}/2")
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> findByPage2(@PathParam("pageIdx") Integer currPage,
                                           @PathParam("pageSize") Integer size) {
        return repository.page2(size, currPage);
    }

    @GET
    @Path("page/{idxStart}/{idxEnd}/3")
    @Produces(MediaType.APPLICATION_JSON)
    public List<AppUserEntity> findByPage3(@PathParam("idxStart") Integer currPage,
                                           @PathParam("idxEnd") Integer size) {
        return repository.page3(size, currPage);
    }
}
